# Maintainer: Alexey Pavlov <Alexpux@gmail.com>

_realname=gimp3
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.99.16
pkgrel=2
pkgdesc="GNU Image Manipulation Program (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang32' 'clang64' 'clangarm64')
url="https://www.gimp.org/downloads/devel/"
license=('spdx:GPL-3.0-or-later AND LGPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-aalib"
         "${MINGW_PACKAGE_PREFIX}-appstream-glib"
         "${MINGW_PACKAGE_PREFIX}-atk"
         "${MINGW_PACKAGE_PREFIX}-babl"
         "${MINGW_PACKAGE_PREFIX}-brotli"
         "${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-drmingw"
         "${MINGW_PACKAGE_PREFIX}-gegl"
         "${MINGW_PACKAGE_PREFIX}-gexiv2"
         "${MINGW_PACKAGE_PREFIX}-ghostscript"
         "${MINGW_PACKAGE_PREFIX}-gi-docgen"
         "${MINGW_PACKAGE_PREFIX}-glib-networking"
         "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
         "${MINGW_PACKAGE_PREFIX}-gobject-introspection-runtime"
         "${MINGW_PACKAGE_PREFIX}-graphviz"
         "${MINGW_PACKAGE_PREFIX}-gtk3"
         "${MINGW_PACKAGE_PREFIX}-headers-git"
         "${MINGW_PACKAGE_PREFIX}-iso-codes"
         "${MINGW_PACKAGE_PREFIX}-json-c"
         "${MINGW_PACKAGE_PREFIX}-json-glib"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-lensfun"
         "${MINGW_PACKAGE_PREFIX}-libarchive"
         "${MINGW_PACKAGE_PREFIX}-libheif"
         "${MINGW_PACKAGE_PREFIX}-libjxl"
         "${MINGW_PACKAGE_PREFIX}-libmng"
         "${MINGW_PACKAGE_PREFIX}-libmypaint"
         "${MINGW_PACKAGE_PREFIX}-libspiro"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-libwmf"
         $([[ ${MSYSTEM_CARCH} == aarch64 ]] || echo "${MINGW_PACKAGE_PREFIX}-luajit")
         "${MINGW_PACKAGE_PREFIX}-lua51"
         "${MINGW_PACKAGE_PREFIX}-lua51-lgi"
         "${MINGW_PACKAGE_PREFIX}-maxflow"
         "${MINGW_PACKAGE_PREFIX}-mypaint-brushes"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-pango"
         "${MINGW_PACKAGE_PREFIX}-poppler"
         "${MINGW_PACKAGE_PREFIX}-poppler-data"
         "${MINGW_PACKAGE_PREFIX}-python-gobject"
         "${MINGW_PACKAGE_PREFIX}-shared-mime-info"
         "${MINGW_PACKAGE_PREFIX}-suitesparse"
         "${MINGW_PACKAGE_PREFIX}-vala"
         "${MINGW_PACKAGE_PREFIX}-xpm-nox")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-meson")
source=("https://download.gimp.org/pub/gimp/v2.99/gimp-${pkgver}.tar.xz"
        0001-clang-rc-files-fix.patch
        0002-skip-test-env.patch
        0004-clang-windres.patch
        lua.interp
        luajit.interp
        gimp-script-fu-interpreter.interp
        )
sha256sums=('6b4496edee447339f923276755247eadb64ec40d8aec241d06b62d1a6eb6508d'
            '54b3abc136b78fd2ae9b7d2acd202fb91bf3b6c248b131f1629ca50ef2493fbe'
            '511a2a969d1ff3254b4f2453cdfbc3a86a60e2379adf9f6ed5735454a45ef6ee'
            '739b15faebe85db54bc09b8556f8eaf3b59789cf26f65a7a17eb36314fb6ceb4'
            '1f3ddde21613be29e83c39ec3fa420073c522e746455997529ada707b7d33eed'
            'ee8fd9e26675c7c2e477f280f3aad20cfa66d0a2da0cbceb7b2448f4bb8f8d37'
            '49d7ac87f2f437e89bb43e9be749c206d4c51bd9245133ffb1071aad4592ffb0')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/gimp-${pkgver}"
  apply_patch_with_msg \
    0001-clang-rc-files-fix.patch \
    0002-skip-test-env.patch

  # Workaround for https://github.com/llvm/llvm-project/issues/57334
  apply_patch_with_msg \
    0004-clang-windres.patch
}

build() {
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a extra_config
  if [[ $MSYSTEM_CARCH == aarch64 ]]; then
    extra_config+=("-Dlua=disabled")
  fi

  CFLAGS+=" -Wno-error=int-conversion -Wno-incompatible-function-pointer-types" \
  CXXFLAGS+=" -Wno-error=int-conversion -Wno-incompatible-function-pointer-types" \
  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson.exe setup \
    --buildtype=plain \
    --auto-features=enabled \
    --prefix=${MINGW_PREFIX} \
    -Daa=disabled \
    -Dalsa=disabled \
    -Dappdata-test=disabled \
    -Ddirectx-sdk-dir="${MINGW_PREFIX}" \
    -Denable-multiproc=true \
    -Dg-ir-doc=false \
    -Dgi-docgen=disabled \
    -Dgudev=disabled \
    -Dheadless-tests=disabled \
    -Djavascript=disabled \
    -Dlinux-input=disabled \
    -Dxcursor=disabled \
    -Dwin32-debug-console=true \
    "${extra_config[@]}" \
    ../gimp-${pkgver}

  ${MINGW_PREFIX}/bin/meson.exe compile
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  ${MINGW_PREFIX}/bin/meson.exe install --destdir="${pkgdir}"

  # Adjust the python interpreter path
  sed -e "s|\=.*|\=python3w.exe|g" -i "${pkgdir}${MINGW_PREFIX}"/lib/gimp/2.99/interpreters/pygimp.interp
  sed -e "s|::python3|::python3w.exe|g" -i "${pkgdir}${MINGW_PREFIX}"/lib/gimp/2.99/interpreters/pygimp.interp

  # Create the scm scripts interpreter path (Only created by the installer in Windows)
  install -Dm644 "${srcdir}"/gimp-script-fu-interpreter.interp "${pkgdir}${MINGW_PREFIX}"/lib/gimp/2.99/interpreters/gimp-script-fu-interpreter.interp

  # Create the Lua scripts interpreter path (Only created by the installer in Windows)
  if [[ $MSYSTEM_CARCH == aarch64 ]]; then
    install -Dm644 "${srcdir}"/lua.interp "${pkgdir}${MINGW_PREFIX}"/lib/gimp/2.99/interpreters/lua.interp
  else
    install -Dm644 "${srcdir}"/luajit.interp "${pkgdir}${MINGW_PREFIX}"/lib/gimp/2.99/interpreters/lua.interp
  fi

  rm -f "${pkgdir}${MINGW_PREFIX}/lib/gimp/2.99/modules/*.dll.a"

  install -Dm644 "${srcdir}/gimp-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
  install -Dm644 "${srcdir}/gimp-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
